package GUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import GUI.manager.VentanaFactory;
import GUI.manager.VentanaManager;

import core.model.Carrito;
import core.model.Lista;

public class panelCrearListaEconomica extends javax.swing.JPanel implements
		ActionListener {
	
	private VentanaManager ventanaManager=VentanaFactory.getInstance();
	private Lista lista= ventanaManager.findbyTitulo(panelCrearLista.titulo);
	
	
	
	private java.util.List<Carrito> carritos=ventanaManager.findByLista(lista.getCodigo_lista());
	private DefaultTableModel modelo=new DefaultTableModel(convertirArray(carritos), new Object[]{"Nombre","Empresa","Cantidad","Precio"});

	public panelCrearListaEconomica(int num) {
		initComponents(num);
	}

	private void initComponents(int num) {
		
		ant=num;
		bTitulo = new javax.swing.JButton();
        etiquetaNick = new javax.swing.JLabel();
        botonSalir = new javax.swing.JButton();
        panelCentro = new javax.swing.JPanel();
        bAnadir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        eImporte = new javax.swing.JLabel();
        eImporteTotal = new javax.swing.JLabel();
        bEnviarLista = new javax.swing.JButton();
        bGuardarHistorial = new javax.swing.JButton();
        eTituloLista = new javax.swing.JLabel();
        eTitulo1 = new javax.swing.JLabel();
        etiquetaEntrada = new javax.swing.JLabel();
        bCrearLista = new javax.swing.JButton();
        bOpciones = new javax.swing.JButton();
        bSugerencias = new javax.swing.JButton();
        bHistorial = new javax.swing.JButton();
        bAyuda = new javax.swing.JButton();
        bBusqueda = new javax.swing.JButton();

		setBackground(new java.awt.Color(255, 255, 255));

		bTitulo.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/encabezadoMain.png")); // NOI18N
		bTitulo.setBorder(null);
		bTitulo.setBorderPainted(false);
		bTitulo.addActionListener(this);
		bTitulo.setBackground(new java.awt.Color(255, 255, 255));

		bSugerencias.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bSugerencias.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonSugerencias.png")); // NOI18N
		bSugerencias.setBorderPainted(false);
		bSugerencias.addActionListener(this);
		bSugerencias.setBackground(new java.awt.Color(255, 255, 255));

		bCrearLista.setBackground(new java.awt.Color(255, 255, 255));
		bCrearLista.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bCrearLista.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonCrearLista.png")); // NOI18N
		bCrearLista.setBorderPainted(false);
		bCrearLista.addActionListener(this);

		bHistorial.setBackground(new java.awt.Color(255, 255, 255));
		bHistorial.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bHistorial.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonHistorial.png")); // NOI18N
		bHistorial.setBorderPainted(false);
		bHistorial.addActionListener(this);

		etiquetaNick.setBackground(new java.awt.Color(102, 102, 102));
		etiquetaNick.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaNick.setText(panelPrincipal.nick);
		etiquetaNick.setToolTipText("");

		botonSalir.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/menu-salir.png")); // NOI18N
		botonSalir.setBorderPainted(false);
		botonSalir.addActionListener(this);
		botonSalir.setBackground(new java.awt.Color(255, 255, 255));

		panelCentro.setBackground(new java.awt.Color(255, 255, 255));

		bAnadir.setBackground(new java.awt.Color(255, 255, 255));
		bAnadir.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		bAnadir.setForeground(new java.awt.Color(243, 15, 120));
		bAnadir.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonAgregar.png")); // NOI18N
		bAnadir.setBorder(null);
		bAnadir.addActionListener(this);
		
		if(panelHistorial.autor==0){
			bAnadir.setEnabled(false);
		}

		jTable1.setModel(modelo);
		jScrollPane1.setViewportView(jTable1);

		eImporte.setBackground(new java.awt.Color(255, 255, 255));
		eImporte.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eImporte.setForeground(new java.awt.Color(102, 102, 102));
		eImporte.setText("IMPORTE TOTAL");
		eImporte.setToolTipText("");

		eImporteTotal.setBackground(new java.awt.Color(255, 255, 255));
		eImporteTotal.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		eImporteTotal.setForeground(new java.awt.Color(243, 15, 120));
		float total=calcularImporte();
		
		
		eImporteTotal.setText(String.valueOf(total));
		

		bEnviarLista.setBackground(new java.awt.Color(255, 255, 255));
		bEnviarLista.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		bEnviarLista.setForeground(new java.awt.Color(243, 15, 120));
		bEnviarLista.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonEnviar.png")); // NOI18N
		bEnviarLista.setBorder(null);
		bEnviarLista.addActionListener(this);

		bGuardarHistorial.setBackground(new java.awt.Color(255, 255, 255));
		bGuardarHistorial
				.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		bGuardarHistorial.setForeground(new java.awt.Color(243, 15, 120));
		bGuardarHistorial.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonGuardar.png")); // NOI18N
		bGuardarHistorial.setBorder(null);
		bGuardarHistorial.addActionListener(this);

		eTituloLista.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
        eTituloLista.setForeground(new java.awt.Color(243, 15, 120));
        eTituloLista.setText("TU LISTA ES");

        eTitulo1.setBackground(new java.awt.Color(102, 102, 102));
        eTitulo1.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
        eTitulo1.setForeground(new java.awt.Color(0,0,0));
        eTitulo1.setText(panelCrearLista.titulo);
        
        org.jdesktop.layout.GroupLayout panelCentroLayout = new org.jdesktop.layout.GroupLayout(panelCentro);
        panelCentro.setLayout(panelCentroLayout);
        panelCentroLayout.setHorizontalGroup(
            panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelCentroLayout.createSequentialGroup()
                .add(49, 49, 49)
                .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(panelCentroLayout.createSequentialGroup()
                        .add(eTituloLista)
                        .add(18, 18, 18)
                        .add(eTitulo1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 241, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, panelCentroLayout.createSequentialGroup()
                        .add(0, 89, Short.MAX_VALUE)
                        .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, panelCentroLayout.createSequentialGroup()
                                .add(eImporteTotal)
                                .add(111, 111, 111))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, panelCentroLayout.createSequentialGroup()
                                .add(bEnviarLista)
                                .add(51, 51, 51)
                                .add(bGuardarHistorial)
                                .add(192, 192, 192))))
                    .add(panelCentroLayout.createSequentialGroup()
                        .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(panelCentroLayout.createSequentialGroup()
                                .add(8, 8, 8)
                                .add(bAnadir)
                                .add(162, 162, 162)
                                .add(eImporte)))
                        .addContainerGap())))
        );
        panelCentroLayout.setVerticalGroup(
            panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, panelCentroLayout.createSequentialGroup()
                .addContainerGap()
                .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(eTituloLista)
                    .add(eTitulo1))
                .add(44, 44, 44)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 137, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(bAnadir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(eImporte)
                        .add(eImporteTotal)))
                .add(18, 18, 18)
                .add(panelCentroLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(bEnviarLista, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(bGuardarHistorial, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(210, Short.MAX_VALUE))
        );

		bOpciones.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bOpciones.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonOpciones.png")); // NOI18N
		bOpciones.setBorderPainted(false);
		bOpciones.addActionListener(this);
		bOpciones.setBackground(new java.awt.Color(255, 255, 255));

		etiquetaEntrada.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaEntrada.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaEntrada.setText("BIENVENIDO A LISTA DE LA COMPRA MAKER ");

		bBusqueda.setBackground(new java.awt.Color(255, 255, 255));
		bBusqueda.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bBusqueda.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonBusqueda.png")); // NOI18N
		bBusqueda.setToolTipText("");
		bBusqueda.setBorderPainted(false);
		bBusqueda.addActionListener(this);

		bAyuda.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bAyuda.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonAyuda.png")); // NOI18N
		bAyuda.setBorderPainted(false);
		bAyuda.addActionListener(this);
		bAyuda.setBackground(new java.awt.Color(255, 255, 255));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout
										.createSequentialGroup()
										.addContainerGap()
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.LEADING)
												.add(layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING)
														.add(bCrearLista,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																157,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(bBusqueda,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																155,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(bHistorial,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																157,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(bSugerencias,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																157,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(bOpciones,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																157,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
												.add(org.jdesktop.layout.GroupLayout.TRAILING,
														bAyuda,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
														157,
														org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.add(35, 35, 35)
										.add(panelCentro,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE))
								.add(layout.createSequentialGroup()
										.add(96, 96, 96).add(bTitulo)
										.add(44, 44, 44).add(botonSalir))
								.add(layout
										.createSequentialGroup()
										.add(84, 84, 84)
										.add(etiquetaEntrada)
										.add(46, 46, 46)
										.add(etiquetaNick,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												280,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(6, 6, 6)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(bTitulo,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										104,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(botonSalir))
						.add(6, 6, 6)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(etiquetaEntrada).add(etiquetaNick))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout
										.createSequentialGroup()
										.add(bBusqueda,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bCrearLista,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bHistorial,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bSugerencias,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bOpciones,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(18, 18, 18)
										.add(bAyuda,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												41,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.add(panelCentro,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
	}
	
	public String[][] convertirArray(java.util.List<Carrito>carritos)
	{
		String [][] datos=new String [carritos.size()][4];
		for(int i=0;i<carritos.size();i++)
		{
			datos[i][0]=String.valueOf(carritos.get(i).getNombre_producto());
			datos[i][1]=String.valueOf(carritos.get(i).getNombre_empresa());
			datos[i][2]=String.valueOf(carritos.get(i).getCantidad());
			datos[i][3]=String.valueOf(carritos.get(i).getPrecio()*carritos.get(i).getCantidad());
			
		}
		return datos;
	}
	
	public float calcularImporte(){
		DefaultTableModel dtm=new DefaultTableModel();
		dtm=(DefaultTableModel) jTable1.getModel();
		
		float total=0;
		
		for(int i=0;i<dtm.getRowCount();i++){
			float precio=Float.parseFloat((String) dtm.getValueAt(i, 3));
			total=total+precio;
			
		}
		return total;
		
	}

	 private javax.swing.JButton bAnadir;
	    private javax.swing.JButton bAyuda;
	    private javax.swing.JButton bBusqueda;
	    private javax.swing.JButton bCrearLista;
	    private javax.swing.JButton bEnviarLista;
	    private javax.swing.JButton bGuardarHistorial;
	    private javax.swing.JButton bHistorial;
	    private javax.swing.JButton bOpciones;
	    private javax.swing.JButton bSugerencias;
	    private javax.swing.JButton bTitulo;
	    private javax.swing.JButton botonSalir;
	    private javax.swing.JLabel eImporte;
	    private javax.swing.JLabel eImporteTotal;
	    private javax.swing.JLabel eTitulo1;
	    private javax.swing.JLabel eTituloLista;
	    private javax.swing.JLabel etiquetaEntrada;
	    private javax.swing.JLabel etiquetaNick;
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JTable jTable1;
	    private int ant;
	   
	private javax.swing.JPanel panelCentro;
	private JFrame actual;
	private JPanel estePanel = this;

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == bSugerencias) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelSugerencias());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}

		if (e.getSource() == bBusqueda) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelBusqueda());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}

		if (e.getSource() == bHistorial) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelHistorial());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bOpciones) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelOpciones());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bAyuda) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelAyuda());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bAnadir) {
			
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelCrearListaBusqueda());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			panelCrearLista.eleccion=1;
			
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bTitulo) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelPrincipal());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == botonSalir) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelSalir(14));
			actual.setPreferredSize(new Dimension(610, 330));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bCrearLista) {
			
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelCrearLista());
			actual.setPreferredSize(new Dimension(850, 630));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}

	}

}
